/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.portal;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

@Entity
@NamedQueries ({
	@NamedQuery(name = "Property.findByName", query = "select o from Property o where o.portal.id = :portalid and o.name = :name"),
	@NamedQuery(name = "Property.findByNameAndValue", query = "select o from Property o where o.name = :name and o.value = :value")
})
@Table (uniqueConstraints = {@UniqueConstraint(columnNames={"name","portal_id"})})
public class Property implements java.io.Serializable,java.lang.Comparable<Property>
{
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(Property.class.getName());
	Integer id;
	Portal portal;
	String name,value;
	public Property() {}
	public Property(Portal portal, String name, String value)
	{
		this.portal=portal;
		this.name = name;
		this.value = value;
	}
	@Id @GeneratedValue
	public Integer getId() {		return id;}
	public String getName()		{	return name;}
	public String getValue()	{	return value;}
	public boolean equals(Object o)
	{
		return ((Property)o).getId().equals(id);
	}
	public int compareTo(Property o)
	{
		return (name+portal.getId()).compareTo(o.getName()+o.getPortal().getId());
	}
	@Transient
	public boolean getBooleanValue()
	{
		try
		{
			return new Boolean(getValue()).booleanValue();
		}catch (Throwable e)
		{
			log.error("Not boolean value for property "+getName()+"@"+portal.getId()+": "+getValue()+"; returning false");
			return false;
		}
	}

	public void setValue(String value)
	{
		this.value = value;
	}

	public void setPortal(Portal portal)
	{
		this.portal = portal;
	}

	@ManyToOne(fetch=FetchType.LAZY, optional=false)
	public Portal getPortal()
	{
		return portal;
	}

	@Id @GeneratedValue
	public void setId(Integer id)
	{
		this.id = id;
	}

	public void setName(String name)
	{
		this.name = name;
	}
}
